define(['zepto', 'utils', 'wxshare', 'JST', 'page/jst/movement-btn', 'page/jst/course-action-group', 'sui'], function($, utils, wxshare, JST, movementBind, actionGroup) {
  return {
    init: function(data) {
      this.roundId = data.roundId;
      this.batchId = data.batchId;
      this.courseCoursewareId = data.courseCoursewareId;
      this.session = data.session;
      
      
      this.onEditMovement();
      this.onClickAllSelect();
      this.bindDelete();
      this.batchLink();
      
      actionGroup.init(window.__CTX__, data)
    },
    
    onEditMovement: function() {
      $(document).on("click", ".edit-button", function() {
        if(!$(this).data("delete")) {
          $(this).find("span").html("取消")
          $(".add-action-box").hide();
          $(".delete-movement").show();
          $(".delete-box").show();
          $(this).data("delete", true)
        } else {
          $(this).find("span").html("删除")
          $(".add-action-box").show();
          $(".delete-movement").hide();
          $(".delete-box").hide();
          $(this).data("delete", false)
        }
      })
    },
    
    onClickAllSelect: function() {
      $(document).on("click", '.all-select', function() {
        if($(this).prop("checked")) {
          $(".delete-movement input[type='checkbox']").prop("checked", true)
        } else {
        	$(".delete-movement input[type='checkbox']").prop("checked", false)
        }
      })
    },
    
    bindDelete: function() {
      var that = this;
      $(document).on("click", ".delete-button", function() {
        $.ajax({
          url: window.__CTX__.root + '/rounds/' + that.roundId + '/course-coursewares/' + that.courseCoursewareId + '/coursewares',
          type: 'POST',
          data: $("form[name='delete_form']").serializeArray(),
          success: function(res) {
            var res = JSON.parse(res)
            if(res.code == "0") {
              window.location.reload();
            } else {
              $.toast(res.message);
            }
          },
          fail: function() {
            $.toast("请求失败");
          }
        })
      })
    },
    
    batchLink: function() {
        var that = this;
        
        $(document).on('click','.batch-link', function () {
            $.modal({
              title: '发送课件',
              text: '<div class="list-block no-margin">' +
                      '<ul>'+
                      '<li>'+
                          '<div class="item-content">'+
                              '<div class="item-inner">'+
                                  '<div class="item-input"><textarea name="coachTips" placeholder="您有任何Tips,请在此输入"></textarea></div>'+
                              '</div>'+
                          '</div>'+
                      '</li>'+
                  '</ul>'+
              '</div>',
              buttons: [
                {
                  text: '确定',
                  onClick: function() {
                      that.bindLinkClick();
                  }
                },
                {
                  text: '取消'
                },
              ]
            })
        });
    },
    
    bindLinkClick: function() {
        var that = this;
        var coachTips = $("textarea[name='coachTips']").val().trim();
        
        var currentData = {'_method': 'PATCH', 'type': 'COURSE', 'day': that.session, 'coachTips': coachTips};
        
        $.ajax({
            url: window.__CTX__.root + '/rounds/' + that.roundId + '/batch-coursewares/' + that.batchId + '/link',
            type: 'POST',
            data:currentData,
            success: function(res) {
                var res = JSON.parse(res);
                $.toast(res.message);
                window.location.reload();
            },
            fail: function() {
                $.toast("请求失败");
            }
        })
    }
  }
})